package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Department;
import cn.wolfcode.mapper.DepartmentMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void delete(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Department department) {
        departmentMapper.insert(department);
    }

    @Override
    public Department selectOne(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Department> selectAll() {
        return departmentMapper.selectAll();
    }

    @Override
    public void update(Department department) {
        departmentMapper.updateByPrimaryKey(department);
    }

    @Override
    public PageInfo<Department> query(QueryObject qo) {
        /*int count = departmentMapper.selectForCount(qo);

        if(count == 0){
            PageResult<Department> pageResult = new PageResult<>(qo.getCurrentPage(), qo.getPageSize(), 0, Collections.emptyList());
            return pageResult;
        }

        int totalPage = count%qo.getPageSize()== 0
                ? count/qo.getPageSize() : count/qo.getPageSize()+1;

        if(qo.getCurrentPage()>totalPage){
            qo.setCurrentPage(totalPage);
        }*/
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());

        List<Department> departments = departmentMapper.selectForList(qo);
        //return new PageResult(qo.getCurrentPage(),qo.getPageSize(),count,departments);
        return new PageInfo<Department>(departments);
    }
}
